package com.industrial.app.service.impl;


import com.industrial.app.domain.formal.BizKeyAreasValuesFormal;
import com.industrial.app.mapper.BizKeyAreasValuesFormalMapper;
import com.industrial.app.service.*;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.DictUtils;
import com.industrial.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AppIndexServiceImpl implements IAppIndexService {

    /**
     *  重点地区工业增加值 */
    @Autowired
    private IBizKeyAreasValuesFormalService bizKeyAreasValuesService;

    /**
     * 重点地区产销率同比
     */
    @Autowired
    private IBizProduceSaleFormalService bizProduceSaleService;

    /**
     * 分行业增加值同比
     */
    @Autowired
    private IBizIndustryAddedFormalService bizIndustryAddedService;

    /**
     * 分市工业增加值同比
     */
    @Autowired
    private IBizIndustrialValueAddedByCityFormalService bizIndustrialValueAddedByCityService;

    /**
     * 重点调度产品产量
     */
    @Autowired
    private IBizFocusSchedulingProductProductionFormalService bizFocusSchedulingProductProductionService;

    /**
     * 分市工业企业主要经济
     */
    @Autowired
    private IBizCityIndustrialEconomicsFormalService bizCityIndustrialEconomicsService;

    /**
     * 分行业工业企业主要经济
     */
    @Autowired
    private  IBizIndustrialSectorMetricsFormalService bizIndustrialSectorMetricsService;
    /**
     * 山东与全国制造采购
     */
    @Autowired
    private IBizManufacturingPmiComparisonFormalService bizManufacturingPmiComparisonService;


    /**
     * 山东制造业PMI一览表
     */
    @Autowired
    private IBizPmiFormalService bizPmiService;

    @Autowired
    private BizKeyAreasValuesFormalMapper bizKeyAreasValuesFormalMapper;

    /**
     * 全社会天线情况
     */
    @Autowired
    private IBizSocialElectricityConsumptionFormalService bizSocialElectricityConsumptionService;

    /**
     * 工业用电量
     */
    @Autowired
    private IBizPowerConsumptionFormalService bizPowerConsumptionService;


    /**
     * @return
     */
    @Override
    public Map<String, Object> getIndexMenu() {
        Map<String,Object> result = new HashMap<>(16);
        /* 重点地区工业增加值 */
        List<String> uniqueDatesFromDatabase = bizKeyAreasValuesService.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            result.put("zddqgyzjz",uniqueDatesFromDatabase.get(0));
        }else{
            result.put("zddqgyzjz","无数据");
        }
        /* 重点地区产销率同比 */
        List<String> zddqcxltbList = bizProduceSaleService.getUniqueDatesFromDatabase();
        if(!zddqcxltbList.isEmpty()){
            result.put("zddqcxltb",zddqcxltbList.get(0));
        }else{
            result.put("zddqcxltb","无数据");
        }
        /* 分市工业增加值同比 */
        List<String> fzgyzjztb = bizIndustrialValueAddedByCityService.getUniqueDatesFromDatabase();
        if(!fzgyzjztb.isEmpty()){
            result.put("fzgyzjztb",fzgyzjztb.get(0));
        }else{
            result.put("fzgyzjztb","无数据");
        }

        /* 分行业增加值同比 */
        List<String> zhyzjztb = bizIndustryAddedService.getUniqueDatesFromDatabase();
        if(!zhyzjztb.isEmpty()){
            result.put("zhyzjztb",zhyzjztb.get(0));
        }else{
            result.put("zhyzjztb","无数据");
        }
        /* 重点调度产品产量 */
        List<String> zddycpcl = bizFocusSchedulingProductProductionService.getUniqueDatesFromDatabase();
        if(!zddycpcl.isEmpty()){
            result.put("zddycpcl",zddycpcl.get(0));
        }else{
            result.put("zddycpcl","无数据");
        }

        /* 分市工业企业主要经济 */
        List<String> fsgyqyzyjj = bizCityIndustrialEconomicsService.getUniqueDatesFromDatabase();
        if(!fsgyqyzyjj.isEmpty()){
            result.put("fsgyqyzyjj",fsgyqyzyjj.get(0));
        }else{
            result.put("fsgyqyzyjj","无数据");
        }

        List<String> sdyqgzzycg = bizManufacturingPmiComparisonService.getUniqueDatesFromDatabase();
        if(!sdyqgzzycg.isEmpty()){
            result.put("sdyqgzzycg",sdyqgzzycg.get(0));
        }else{
            result.put("sdyqgzzycg","无数据");
        }
        List<String> sdzzypmi = bizPmiService.getUniqueDatesFromDatabase();
        if(!sdzzypmi.isEmpty()){
            result.put("sdzzypmi",sdzzypmi.get(0));
        }else{
            result.put("sdzzypmi","无数据");
        }
        List<String> qshydlqk = bizSocialElectricityConsumptionService.getUniqueDatesFromDatabase();
        if(!qshydlqk.isEmpty()){
            result.put("qshydlqk",qshydlqk.get(0));
        }else{
            result.put("qshydlqk","无数据");
        }

        List<String> gyydlqk = bizPowerConsumptionService.getUniqueDatesFromDatabase();
        if(!gyydlqk.isEmpty()){
            result.put("gyydlqk",gyydlqk.get(0));
        }else{
            result.put("gyydlqk","无数据");
        }

        /* 分行业工业企业主要经济指标 */
        List<String> fhygyqyzyjj = bizIndustrialSectorMetricsService.getUniqueDatesFromDatabase();
        if(!fhygyqyzyjj.isEmpty()){
            result.put("fhygyqyzyjj",fhygyqyzyjj.get(0));
        }else{
            result.put("fhygyqyzyjj","无数据");
        }
        return result;
    }

    /**
     * @return
     */
    @Override
    public AjaxResult quetyShengFen() {

        List<SysDictData> import_area = DictUtils.getDictCache("import_area");

        return AjaxResult.success(import_area);
    }

    /**
     * @param params
     * @return
     */
    @Override
    public AjaxResult queryZddqgyzjztb(Map<String, String> params) {
        if(StringUtils.isBlank(params.get("baseArea")) || StringUtils.isBlank(params.get("dates"))){
            return AjaxResult.warn("没有找到需要查询的地区或日期");
        }
        String baseArea = params.get("baseArea");
        String dates = params.get("dates");

        BizKeyAreasValuesFormal param = new BizKeyAreasValuesFormal();

        param.setBaseArea(baseArea);
        param.setDates(dates);
        String areaName = DictUtils.getDictValue("import_area", baseArea);
        List<BizKeyAreasValuesFormal> bizKeyAreasValuesFormals = bizKeyAreasValuesFormalMapper.selectIndexGyZjzTbList(param);
        Map<String, BizKeyAreasValuesFormal> collect = new HashMap<>();
        if(bizKeyAreasValuesFormals != null && !bizKeyAreasValuesFormals.isEmpty()){
            collect = bizKeyAreasValuesFormals.stream().collect(Collectors.toMap(k -> {
                String datess = k.getBaseMonth();
                return datess.split("-")[1];
            }, v -> v));
        }
        List<BizKeyAreasValuesFormal> resultList = new ArrayList<>();
        for(int i = 1; i <= 12;i++){
            String formattedNumber = String.format("%02d", i);
            if(collect.containsKey(formattedNumber)){
                BizKeyAreasValuesFormal bizKeyAreasValuesFormal = collect.get(formattedNumber);
                bizKeyAreasValuesFormal.setBaseMonth(formattedNumber + "月");
                resultList.add(bizKeyAreasValuesFormal);
            }else{
                BizKeyAreasValuesFormal temp = new BizKeyAreasValuesFormal();
                temp.setBaseMonth(formattedNumber + "月");
                temp.setCurrentMonth(null);
                temp.setAccumulate(null);
                temp.setBaseArea(areaName);
                resultList.add(temp);
            }
        }
        return AjaxResult.success(resultList);
    }


}
